package com.example.myapplication.shortvideo.utils;

import android.text.TextUtils;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;

/**
 * @author zijian.cheng
 * @date on 2018/7/30.
 */
public class DateUtils {


    /**
     * 秒与毫秒的倍数
     */
    public static final long SEC = 1000;
    /**
     * 分与毫秒的倍数
     */
    public static final long MIN = SEC * 60;
    /**
     * 时与毫秒的倍数
     */
    public static final long HOUR = MIN * 60;
    /**
     * 天与毫秒的倍数
     */
    public static final long DAY = HOUR * 24;

    /**
     * 周与毫秒的倍数
     */
    public static final long WEEK = DAY * 7;

    /**
     * 月与毫秒的倍数
     */
    public static final long MONTH = DAY * 30;

    /**
     * 年与毫秒的倍数
     */
    public static final long YEAR = DAY * 365;

    /**
     * 20130225
     */
    public static final int FORMAT_TO_DAY = 0;
    /**
     * 2013022511
     */
    public static final int FORMAT_TO_HOUR = 1;
    /**
     * 201302251134
     */
    public static final int FORMAT_TO_MINUTE = 2;
    /**
     * 20130225113420
     */
    public static final int FORMAT_TO_SECOND = 3;
    /**
     * 20130225113420888
     */
    public static final int FORMAT_TO_MILLI = 4;
    /**
     * 04-18 13:00
     */
    public static final int FORMAT_TO_CUSTOM1 = 5;
    /**
     * 04-18 13:00
     */
    public static final int FORMAT_TO_CUSTOM2 = 6;

    /**
     * 0字符串表示
     */
    public static final String ZERO_SINGLE_STRING = "0";

    /**
     * 00 字符串表示
     */
    public static final String ZERO_DOUBLE_STRING = "00";

    private static final String[] SIMPLE_TIME_FORMAT = {
            "yyyy-MM-dd", "yyyy-MM-dd-HH", "yyyy-MM-dd-HH-mm", "yyyy-MM-dd-HH-mm-ss", "yyyy-MM-dd-HH-mm-ss-SSS",
            "MM-dd HH:mm", "MM-dd"
    };

    /**
     * 转换当前日期时间为字符串 2013022511
     *
     * @param format 转换后的格式类型
     * @return 转换后的字符
     */
    public static String formatcurrentDate(int format) {
        return formatDate(new Date(), format, "");
    }

    /**
     * 转换当前日期时间为字符串 2013{delimiter}02{delimiter}25{delimiter}11
     *
     * @param format    转换后的格式类型
     * @param delimiter 分隔符
     * @return 转换后的字符
     */
    public static String currentTimeToString(int format, String delimiter) {
        return formatDate(new Date(), format, delimiter);
    }

    /**
     * 转换指定日期时间为字符串 2013022511
     *
     * @param timeMilliSeconds 微秒单位的日期时间
     * @param format           转换后的格式类型。
     *                         <p>{@link #FORMAT_TO_DAY}</p>
     *                         <p>{@link #FORMAT_TO_HOUR}</p>
     *                         <p>{@link #FORMAT_TO_MINUTE}</p>
     *                         <p>{@link #FORMAT_TO_SECOND}</p>
     * @return 转换后的字符串
     */
    public static String formatDate(long timeMilliSeconds, int format) {
        return formatDate(new Date(timeMilliSeconds), format, "");
    }

    /**
     * 转换指定日期时间为字符串 2013{delimiter}02{delimiter}25{delimiter}11
     *
     * @param timeMilliSeconds 微秒单位的日期时间
     * @param format           转换后的格式类型。
     *                         <p>{@link #FORMAT_TO_DAY}</p>
     *                         <p>{@link #FORMAT_TO_HOUR}</p>
     *                         <p>{@link #FORMAT_TO_MINUTE}</p>
     *                         <p>{@link #FORMAT_TO_SECOND}</p>
     * @param delimiter        分隔符
     * @return 转换后的字符串
     */
    public static String formatDate(long timeMilliSeconds, int format, String delimiter) {
        return formatDate(new Date(timeMilliSeconds), format, delimiter);
    }

    private static String formatDate(Date date, int format, String delimiter) {
        return formatDate(format, delimiter).format(date);
    }

    private static SimpleDateFormat formatDate(int format, String delimiter) {
        assert format >= 0 && format < SIMPLE_TIME_FORMAT.length;
        String formatString = SIMPLE_TIME_FORMAT[format];
        if (delimiter != null) {
            formatString = formatString.replace("-", delimiter);
        }
        return new SimpleDateFormat(formatString);
    }

    /**
     * 获取指定日期的下一个日期
     *
     * @param date 指定日期
     * @return 下一个日期
     */
    public static Date nextDate(Date date) {
        return new Date(date.getYear(), date.getMonth(), date.getDate() + 1);
    }

    /**
     * 获取指定时间的下一个日期
     *
     * @param timeMilliSeconds 指定时间
     * @return 下一个日期
     */
    public static Date nextDate(long timeMilliSeconds) {
        return nextDate(new Date(timeMilliSeconds));
    }

    /**
     * 获取置顶时间的当天0点的日期
     *
     * @param timeMilliSeconds timeMilliSeconds
     * @return Date
     */
    public static Date currentDate(long timeMilliSeconds) {
        Date date = new Date(timeMilliSeconds);
        return new Date(date.getYear(), date.getMonth(), date.getDate());
    }

    /**
     * 获取当前月
     *
     * @return
     */
    public static int getCurrentMonth() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.MONTH) + 1;
    }

    /**
     * 根据月 获取对应的月份 天数
     */
    public static int getDaysByYearMonth(int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        return a.get(Calendar.DATE);
    }

    public static String timeStampToDate(long timeStamp) {
        Date date = new Date(timeStamp);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = simpleDateFormat.format(date);
        return dateStr;
    }

    public static int getYearByTimeStamp(long timeStamp) {
        String date = timeStampToDate(timeStamp);
        String year = date.substring(0, 4);
        return Integer.parseInt(year);
    }

    /**
     * 根据时间戳获取月份
     *
     * @param timeStamp
     * @return
     */
    public static int getMonthByTimeStamp(long timeStamp) {
        String date = timeStampToDate(timeStamp);
        String month = date.substring(5, 7);
        return Integer.parseInt(month);
    }

    /**
     * 根据时间戳获取月份
     *
     * @param timeStamp
     * @return
     */
    public static String getMonthWithZeroByTimeStamp(long timeStamp) {
        String date = timeStampToDate(timeStamp);
        return date.substring(5, 7);
    }

    public static int getDayByTimeStamp(long timeStamp) {
        String date = timeStampToDate(timeStamp);
        String day = date.substring(8, 10);
        return Integer.parseInt(day);
    }


    /**
     * 判断是否同一天
     *
     * @param millis 毫秒时间戳
     * @return {@code true}: 是<br>{@code false}: 否
     */
    public static boolean isSameDay(long millis) {
        long wee = (System.currentTimeMillis() / DAY) * DAY;
        return millis >= wee && millis < wee + DAY;
    }

    public static boolean isYesterday(long timeMills) {
        GregorianCalendar c1 = new GregorianCalendar();
        c1.setTimeInMillis(timeMills);
        GregorianCalendar c2 = new GregorianCalendar();
        c2.setTimeInMillis(System.currentTimeMillis());

        int y1 = c1.get(Calendar.YEAR);
        int m1 = c1.get(Calendar.MONTH) + 1;
        int d1 = c1.get(Calendar.DAY_OF_MONTH);

        int y2 = c2.get(Calendar.YEAR);
        int m2 = c2.get(Calendar.MONTH) + 1;
        int d2 = c2.get(Calendar.DAY_OF_MONTH);

        return (y1 == y2) && (m1 == m2) && (d2 - d1 == 1);
    }


    public static String formatSeconds(long time, String format) {
        time *= 1000;
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(new Date(time));
    }

}
